/**
 * 辅助指令解码
 * User: Zhikang.Peng
 * Date: 2018/04/09 15:38
 */
package base

import "gitee.com/thghh/jvmgo/rtda"

/**
 *	辅助指令解码
 *  FetchOperands 从字节码中提取操作数
 *  Execute 方法执行指令逻辑
 */
type Instruction interface {
	FetchOperands(reader *BytecodeReader)
	Execute(frame *rtda.Frame)
}

/**
 * 没有操作数的指令
 */
type NoOperandsInstruction struct {
}

func (self *NoOperandsInstruction) FetchOperands(reader *BytecodeReader) {
}

/**
 * 跳转指令
 * Offset 存放跳转偏移量
 */
type BranchInstruction struct {
	Offset int
}

func (self *BranchInstruction) FetchOperands(reader *BytecodeReader) {
	self.Offset = int(reader.ReadInt16())
}

/**
 * 存储和加载类指令需要根据索引存取局部变量表，索引由单字节操作数给出
 * Index 局部变量表索引
 */
type Index8Instruction struct {
	Index uint
}

func (self *Index8Instruction) FetchOperands(reader *BytecodeReader) {
	self.Index = uint(reader.ReadUint8())
}

/**
 * 访问运行时常量池，常量池索引由两字节操作数给出
 * Index 常量表索引
 */
type Index16Instruction struct {
	Index uint
}

func (self *Index16Instruction) FetchOperands(reader *BytecodeReader) {
	self.Index = uint(reader.ReadUint16())
}
